<template>
  <div></div>
</template>

<script>
import { Dialog } from "vant";
export default {
  data() {
    return {
      code: this.$route.query.code,
    };
  },
  methods: {
    async wxLogin() {
      return new Promise((resolve) => {
        this.$axios({
          method: "get",
          url: "/wechat_api/sns/oauth2/access_token",
          params: {
            appid: "wx251bfb5939f895d1",
            secret: "a5683202dc42889372e0ed983244867a",
            code: this.code,
            grant_type: "authorization_code",
          },
        }).then((res) => {
          resolve(res.data);
        });
      });
    },
    login(unionid) {
      this.$axios({
        method: "post",
        url: "/api/adminapi/signin",
        data: {
          signin_type: "unionid",
          unionid: this.$AES.encrypt(unionid),
        },
      }).then((res) => {
        if (res.data.ret == 0) {
          let usertype = res.data.usertypen;
          let is_active = res.data.is_active;
          let username = res.data.username;
          let token = res.data.token;
          let mode = res.data.user_mode;
          // 相关信息放入缓存
          this.$store.dispatch("login", {
            username,
            token,
            usertype,
            is_active,
            unionid,
            mode,
          });
          let usertype_int = parseInt(this.$AES.decrypt(usertype));
          if (usertype_int >= 0) {
            this.$router.replace("/client/home");
          }
        } else if (res.data.ret == 1 || res.data.ret == 3) {
          Dialog.alert({
            title: "提示",
            message: res.data.msg,
          }).then(() => {
            this.$router.replace(`/404?msg=${res.data.msg}`);
          });
        } else if (res.data.ret == 4) {
          this.$store.dispatch("resetUnionid", unionid).then(() => {
            this.$router.replace("/idcard");
          });
        }
      });
    },
  },
  mounted() {
    this.wxLogin().then((login_id) => {
      console.log(login_id);
      localStorage.setItem('extra_tenant_user_openid',login_id.openid);
      this.login(login_id.unionid);
    });
  },
};
</script>

<style scoped>
</style>